﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace NTTDATA.BEENChina.ResultCertification.DataStore
{
    class JaNameClientAccess:IJaNameClientAccess
    {

        private static string ConnectionString
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["ResultCertificationDB"];
            }
        }

        public bool FindName(string name)
        {
            bool bresult = FindKannji(name);

            if (!bresult)
            {
                bresult = FindKana(name);
            }

            return bresult;
        }

        public bool FindKana(string kana)
        {
            string sql = "SELECT COUNT(*)  FROM [JaName] WHERE   [Kana]= @kana";

            SqlParameter[] sqlParameters = 
            {
                new SqlParameter("@kana",  kana)
            };

            try
            {

                string result =
                    SqlHelper.ExecuteScalar(ConnectionString, System.Data.CommandType.Text, sql, sqlParameters).ToString
                        ();

                int count = 0;

                int.TryParse(result, out count);

                return count > 0;
            }
            catch (Exception ex)
            {
                Log.Console.Error("JaNameClientAccess.FindKana", ex);
                return false;
            }
        }

        public bool FindKannji(string kannji)
        {
            string sql = "SELECT COUNT(*)  FROM [JaName] WHERE   [Kannji]= @Kannji";

            SqlParameter[] sqlParameters = 
            {
                new SqlParameter("@Kannji",  kannji)
            };
            try
            {
                string result = SqlHelper.ExecuteScalar(ConnectionString, System.Data.CommandType.Text, sql, sqlParameters).ToString();

                int count = 0;

                int.TryParse(result, out count);

                return count > 0;
            }
            catch (Exception ex)
            {
                Log.Console.Error("JaNameClientAccess.FindKannji", ex);
                return false;
            }
        }

        private List<string> GetNameKana(string kannji, string type)
        {
            string sql = "SELECT  [Kana] FROM [JaName] WHERE   [Kannji]= @Kannji And Type = @Type";

            SqlParameter[] sqlParameters = 
            {
                new SqlParameter("@Kannji",  kannji),
                new SqlParameter("@Type",  type)

            };

            List<string> kanaNames = null;
            try
            {
                using (
                    SqlDataReader reader = SqlHelper.ExecuteReader(ConnectionString, System.Data.CommandType.Text, sql,
                                                                   sqlParameters))
                {
                    while (reader.Read())
                    {
                        if (kanaNames == null)
                            kanaNames = new List<string>();

                        if (!reader.IsDBNull(0))
                        {
                            kanaNames.Add( reader.GetString(0));
                        }
                    }

                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                Log.Console.Error("JaNameClientAccess.GetNameKana", ex);
            }

            return kanaNames;
        }

        public List<string> GetFirstNameKana(string firstName)
        {
            return GetNameKana(firstName, "姓");
        }
        public List<string> GetLastNameKana(string lastName)
        {
            return GetNameKana(lastName, "名");

        }
    }
}
